# -*- coding: UTF-8 -*-
"""
    TweMoi, aplicación Android para la interacción con redes sociales
    Copyright (C) 2011
        * Autor: Moisés Vázquez Sánchez
        * Tutor: Jesús M. González Barahona
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import os

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import models

class Principal(webapp.RequestHandler):
    
    def get(self):
        user = users.get_current_user()
        
        user_query = models.Usuario.all().filter('user_twemoi = ', user.nickname())
        if (user_query.get() != None):
            usuario = user_query.get()
        else:
            u = models.Usuario(author = user,
            user_twemoi = user.nickname(),
            urlImage = "https://twe-moi.appspot.com/archivos/imagenes/perfil_user/user.png")
            u.put()
        
        list_amigos = models.Red.all().filter('user_twemoi = ', user.nickname())
        
        list_amigos_num = models.Amigo.all().filter('user_twemoi = ', user.nickname())

        user_twitter = None
        user_facebook = None
        user_linkedin = None
        user_status = None
        user_buzz = None
        
        for amigo in list_amigos:
            if (amigo.tipo_red == "Twitter"):
                user_twitter = amigo.user_red
            elif (amigo.tipo_red == "Facebook"):
                user_facebook = amigo.user_red
            elif (amigo.tipo_red == "Linkedin"):
                user_linkedin = amigo.user_red
            elif (amigo.tipo_red == "Status"):
                user_status = amigo.user_red
            elif (amigo.tipo_red == "Buzz"):
                user_buzz = amigo.user_red
        
        user_query = models.Usuario.all().filter('user_twemoi = ', user.nickname())
        
        if (user_query.get() != None):
            usuario = user_query.get()
        
        template_values = {
                           'user' : usuario,
                           'num_amigos' : list_amigos_num.count(),
                           'user_twitter' : user_twitter,
                           'user_facebook' : user_facebook,
                           'user_linkedin' : user_linkedin,
                           'user_status' : user_status,
                           'user_buzz' : user_buzz,
                           'link_salir' : users.create_logout_url("/")
                           }
        
        
        path = os.path.join(os.path.dirname(__file__), 'gestion_redes.html')
        self.response.out.write(template.render(path, template_values))



application = webapp.WSGIApplication(
                                     [('/gestion', Principal)
                                      ],debug=True)
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
